<template>
  <div>
    <!-- 标题头部 -->
    <van-nav-bar title="登录" />
    <!-- form表单 -->
    <van-form ref="loginForm" @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="verifyRules.mobile"
        type="number"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>

      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="verifyRules.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>

        <!-- 插槽 -->
        <!-- #button是slot="button"的简写 -->
        <template #button>
        <!-- <template slot="button"> -->
          <van-count-down
            v-if="isCountDownShow"
            :time="60000"
            format="ss s"
            @finish="isCountDownShow = false"
          />
          <van-button
            v-else
            class="but-end"
            round
            size="small"
            type="primary"
            native-type="button"
            @click="onSendSms"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, verification } from "@/api/user";
export default {
  data() {
    return {
      isCountDownShow: false, // 判断倒计时是否显示
      user: {
        mobile: "13140600617", // 手机号
        code: "246810" // 验证码
      },
      // 表单验证
      verifyRules: {
        mobile: [
          { required: true, message: "请填写手机号" },
          { pattern: /^1[3|5|7|8]\d{9}$/, message: "手机号格式错误" }
        ],
        code: [
          { required: true, message: "请填写验证码" },
          { pattern: /^\d{6}$/, message: "验证码格式错误" }
        ]
      }
    };
  },
  methods: {
    // 点击登录
    onSubmit: async function() {
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0
      });
      try {
        const { data: res } = await login(this.user);
        // vuex
        this.$store.commit("setuser", res.data);
        this.$toast.success("登录成功");
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail("手机号或验证码错误");
        } else {
          this.$toast.fail("登录失败，请稍后重试");
        }
      }
    },
    // 发生验证码
    onSendSms: async function() {
      // 1.校验手机号
      try {
        // 调用validate手动校验
        await this.$refs.loginForm.validate("mobile");
      } catch {
        return console.log("验证失败");
      }
      // 2.显示倒计时
      this.isCountDownShow = true;
      // 3.请求发送验证码
      try {
        await verification(this.user.mobile);
        this.$toast("发送成功");
      } catch (err) {
        if (err.response.status === 429) {
          this.$toast("过于频繁，请稍后重试");
        } else {
          this.$toast("发送失败，请稍后重试");
        }
      }
    }
  }
};
</script>

<style lang="less" scoped>
.toutiao {
  font-size: 37px;
}
.login-btn-wrap {
  padding: 55px 33px;
}
.but-end {
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  font-size: 22px;
  color: #666;
  border: none;
}
</style>
